package com.app.ywms.rk.xhqrd

import com.app.bd.corp.BdCorp
import com.app.exception.ValidateException
import com.app.pub.billtype.PubBilltype
import org.springframework.dao.DataIntegrityViolationException


import com.app.exception.BusinessException
import com.app.util.CriteriaHelper
import grails.converters.JSON
import org.hibernate.criterion.Restrictions

import static java.util.UUID.randomUUID
class YxhqrdController {
    //注册pageId   UUID 去除“-”
    def static _pageId=randomUUID().toString().replaceAll('-','')
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def springSecurityService
    def static ref_pageId = randomUUID().toString().replaceAll('-','')
    def pubActionscriptService
    def yxhqrdService

    def index() {
        [pageId:_pageId]
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [yxhqrdInstanceList: Yxhqrd.list(params), yxhqrdInstanceTotal: Yxhqrd.count()]
    }

    /**
     * 进入添加页面
     * @return
     */
    def create(String keys,String businessKey,String sourceBillTypeCode) {
        def yxhqrdInstance= pubActionscriptService.runChangeScript(sourceBillTypeCode,'YXHQRD',[data:keys,'businessKey':businessKey])

        if(sourceBillTypeCode){
            yxhqrdInstance.sourceBillTypeCode=sourceBillTypeCode
            yxhqrdInstance.sourceBillType=PubBilltype.findByBilltypecode(sourceBillTypeCode)
        }
        [yxhqrdInstance: yxhqrdInstance,pageId:_pageId]
    }

/**
 * 保存
 * @return
 */
    def save() {
        def pkCorp = springSecurityService.authentication.loginCorp
        def corp = BdCorp.get(pkCorp)
        def yxhqrdInstance

        if(params.id){

            yxhqrdInstance=Yxhqrd.get(params.id)
            bindData(yxhqrdInstance,params)

        }else
            yxhqrdInstance = new Yxhqrd(params)
        yxhqrdInstance.maker=springSecurityService.currentUser
        yxhqrdInstance.makeDate=new Date()
        yxhqrdInstance.bdCorp = corp
        if (!yxhqrdInstance.validate()) {
            def _errors=[]
            g.eachError(bean: yxhqrdInstance){
                println it
                _errors << [ field:it.field,  message:g.message(error: it)]

            }

            def json=[validateerrors:_errors]

            render json as JSON
            return
        }

            def dt=  yxhqrdService.save(yxhqrdInstance)
            def data=[:]
            data.remoteMessage = message(code: 'default.created.message', args: [message(code: 'yxhqrd.label', default: 'Yxhqrd'), yxhqrdInstance.billno])
            data.rows=dt
            render data as JSON


    }

    /**
     * 显示
     * @param id
     * @return
     */
    def show(String id) {
        def yxhqrdInstance = Yxhqrd.get(id)
        if (!yxhqrdInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'yxhqrd.label', default: 'Yxhqrd'), id])
            redirect(action: "list")
            return
        }

        [yxhqrdInstance: yxhqrdInstance]
    }


    /**
     * 进入编辑页面
     * @param id
     * @return
     */
    def edit(String id) {
        def yxhqrdInstance = Yxhqrd.get(id)
        if (!yxhqrdInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'yxhqrd.label', default: 'Yxhqrd'), id])

        }

        [yxhqrdInstance: yxhqrdInstance,pageId:_pageId]
    }




    /**
     * 删除
     * @param id
     * @return
     */
    def delete(String id) {

        yxhqrdService.delete(id);
        render id

    }

    def createQuery(){
        render(view: 'query',model:  [pageId:_pageId])
    }

    /**
     * 查询
     * @return
     */
    def query(){

        def yxhqrdInstanceLs
        def yxhqrdInstanceTotal
        def corp = springSecurityService.authentication.loginCorp
        if (params.queryParams){
            def json=JSON.parse( URLDecoder.decode(params.queryParams,'utf-8'))

            CriteriaHelper ch = new CriteriaHelper(Yxhqrd.createCriteria(),json)

            def cc=new CriteriaHelper(Yxhqrd.createCriteria(),json)

            def query= pubActionscriptService.runScript('YXHQRD','query',[listQuery:ch,countQuery:cc,targetBill:params.targetBill,loginCorp:corp,loginUser:springSecurityService.getCurrentUser()],0,null)

            yxhqrdInstanceTotal = query.countQuery.count()

            yxhqrdInstanceLs = query.listQuery.list(params,"dgsj")
        }else if (params.q!=null){
            def sjson="""{'operator':'or',
                        'criterias':[
                                    {'operator':'contains','fieldName':'dgsj','value':'${params.q}'},
                                    {'operator':'contains','fieldName':'hc','value':'${params.q}'}
                                    ]
                        }"""


            def json=JSON.parse(sjson);

            CriteriaHelper ch = new CriteriaHelper(Yxhqrd.createCriteria(),json)

            def cc=new CriteriaHelper(Yxhqrd.createCriteria(),json)
            def query= pubActionscriptService.runScript('YXHQRD','query',[listQuery:ch,countQuery:cc,targetBill:params.targetBill,loginCorp:corp,loginUser:springSecurityService.getCurrentUser()],0,null)

            yxhqrdInstanceTotal = query.countQuery.count()

            yxhqrdInstanceLs = query.listQuery.list(params,"dgsj")

        }else if (params.id){
            def yxhqrdInstanceInstance= Yxhqrd.get(params.id)
            def json= [rows: [yxhqrdInstanceInstance], total: 1]
            render json as JSON
            return;
        }
        else{

            CriteriaHelper ch = new CriteriaHelper(Yxhqrd.createCriteria())

            def cc=new CriteriaHelper(Yxhqrd.createCriteria())
            def query= pubActionscriptService.runScript('YXHQRD','query',[listQuery:ch,countQuery:cc,targetBill:params.targetBill,loginCorp:corp,loginUser:springSecurityService.getCurrentUser()],0,null)

            yxhqrdInstanceTotal = query.countQuery.count()

            yxhqrdInstanceLs = query.listQuery.list(params,"dgsj")
        }


        def rt= pubActionscriptService.runScript('YXHQRD','query',[data:yxhqrdInstanceLs],1,null)

        def json= [rows: rt, total: yxhqrdInstanceTotal]
        render json as JSON
    }
    def refPage(String businessKey){
        [pageId:ref_pageId,handler:params.handler,'businessKey':businessKey]
    }

    def check(String id){

        def yxhjhdInstance = Yxhqrd.get(id)


       yxhqrdService.check(yxhjhdInstance)
        def rt=[remoteMessage:'审核成功！',data:[checker:[userRealName:yxhjhdInstance.checker.userRealName],'checkDate':yxhjhdInstance.checkDate]]
        render rt as JSON

    }

    def uncheck(String id){
        def yxhjhdInstance = Yxhqrd.get(id)


       yxhqrdService.uncheck(yxhjhdInstance)
        def rt = [remoteMessage:'弃审成功！',data:[checker:[userRealName:''],'checkDate':'']]
        render rt as JSON
    }

}
